//
//  RefundView.m
//  dyseller
//
//  Created by 红喇叭 on 2017/12/8.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "RefundView.h"

@implementation RefundView
- (instancetype)initWithFrame:(CGRect)frame goodsInfo:(NSArray*)array{
    self = [super initWithFrame:frame];
    if(self){
        _orderInfoArray = array.mutableCopy;
        [self joinIdent];
        [self initView];
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
        
        self.userInteractionEnabled = YES;
        tap.delegate = self;
        [self addGestureRecognizer:tap];

    }
    return self;
}

- (void)joinIdent{
    for(int i=0;i<_orderInfoArray.count;i++){
        NSDictionary *dic = _orderInfoArray[i];
        NSMutableDictionary *newdic = dic.mutableCopy;
        [newdic setObject:@"0" forKey:@"isSelect"];
        _orderInfoArray[i] = newdic;
    }
}

- (void)initView{
    backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GETWITH(600), GETWITH(660))];
    backView.layer.cornerRadius = 10;
    backView.layer.masksToBounds = YES;
    backView.backgroundColor = kWhiteColor;
    backView.center = self.center;
    [self addSubview:backView];
    
    UILabel *alertlab = [[UILabel alloc] initWithFrame:CGRectMake(GETWITH(30), GETHEIGHT(52), GETWITH(250), GETWITH(28))];
    alertlab.text = @"请选择退款商品：";
    alertlab.font = font14;
    alertlab.textAlignment = NSTextAlignmentCenter;
    [backView addSubview:alertlab];
    
    UIButton *allSelect = [[UIButton alloc] initWithFrame:CGRectMake(backView.width-GETWITH(132), GETHEIGHT(46), GETWITH(70), GETHEIGHT(34))];
    allSelect.titleLabel.font = font17;
    [allSelect setTitle:@"全选" forState: 0];
    [allSelect setTitleColor:kBlackColor forState:UIControlStateNormal];
    [allSelect addTarget:self action:@selector(allSelectState) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:allSelect];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, alertlab.bottom+GETHEIGHT(46), backView.width, 0.5)];
    lineView.backgroundColor = kGrayColor;
    [backView addSubview:lineView];
    
    self.selectTableView.frame = CGRectMake(0, lineView.bottom+GETHEIGHT(20), backView.width, GETWITH(380));
    [backView addSubview:_selectTableView];
    
    UIView *lineView2 = [[UIView alloc] initWithFrame:CGRectMake(0, _selectTableView.bottom, backView.width, 0.5)];
    lineView2.backgroundColor = kGrayColor;
    [backView addSubview:lineView2];

    
    UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, lineView2.bottom, backView.width/2,backView.height-lineView2.bottom)];
    [cancleBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn setBackgroundColor:kWhiteColor];
    __weak typeof (self)weakself = self;
    [cancleBtn handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        [weakself removeFromSuperview];
    }];
    [backView addSubview:cancleBtn];
    
    UIButton *sureBtn = [[UIButton alloc] initWithFrame:CGRectMake(backView.width/2, cancleBtn.top, cancleBtn.width, cancleBtn.height)];
    [sureBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
    [sureBtn setBackgroundColor:MianRedColor];
    [sureBtn setTitle:@"确认" forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureSelectList) forControlEvents:UIControlEventTouchUpInside];
    [backView addSubview:sureBtn];

    
    
}

#pragma  mark ========点击事件========
- (void)removeView{
    [self removeFromSuperview];
}

- (void)changeSelectState:(UIButton *)btn{
    NSDictionary *dic = _orderInfoArray[btn.tag];
    NSMutableDictionary *newdic = dic.mutableCopy;
   
    [newdic setObject: [newdic[@"isSelect"] isEqualToString:@"0"]?@"1":@"0" forKey:@"isSelect"];
    _orderInfoArray[btn.tag] = newdic;
    [_selectTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:btn.tag inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)allSelectState{
    for(int i=0;i<_orderInfoArray.count;i++){
        NSDictionary *dic = _orderInfoArray[i];
        NSMutableDictionary *newdic = dic.mutableCopy;
        [newdic setObject:@"1" forKey:@"isSelect"];
        _orderInfoArray[i] = newdic;
    }
    [_selectTableView reloadData];
}

- (void)sureSelectList{
    NSMutableArray *array = @[].mutableCopy;
    for(int i=0;i<_orderInfoArray.count;i++){
        NSDictionary *dic = _orderInfoArray[i];
        NSMutableDictionary *newdic = dic.mutableCopy;
        if([newdic[@"isSelect"] isEqualToString:@"1"]){
            [newdic removeObjectForKey:@"isSelect"];
            [array addObject:newdic];
        }
    }
    if(array.count<1){
        [AlertUtils showHint:@"请选择退款的商品"];
        return;
    }
    
    _selectBlock(array);
     [self removeFromSuperview];
}

#pragma  mark ========懒加载========
- (UITableView *)selectTableView{
    if(_selectTableView == nil){
        _selectTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H-TabBarItemHeight-NavigationHeight+49) style:UITableViewStylePlain];
        _selectTableView.dataSource = self;
        _selectTableView.delegate = self;
        _selectTableView.estimatedRowHeight = 60;
        
        _selectTableView.tableFooterView = [UIView new];
        _selectTableView.tableHeaderView = [UIView new];

    }
    return _selectTableView;
}


#pragma  mark ========UITableViewDelegate,UITableViewDataSource========
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _orderInfoArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"refundCell"];
    NSDictionary *dic = _orderInfoArray[indexPath.row];
  //  cell.textLabel.text = dic[@"goods_name"];
    
    UIButton *selectBtn = [[UIButton alloc] init];
    [selectBtn setImage:[UIImage imageNamed:@"edit_Radio4_default_nor"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"edit_Radio3_default_selected"] forState:UIControlStateSelected];
    selectBtn.tag = indexPath.row;
    if([dic[@"isSelect"] isEqualToString:@"0"]){
        selectBtn.selected = NO;
    }else{
        selectBtn.selected = YES;
    }
    [cell.contentView addSubview:selectBtn];
    [selectBtn addTarget:self action:@selector(changeSelectState:) forControlEvents:UIControlEventTouchUpInside];
    selectBtn.sd_layout.leftSpaceToView(cell.contentView, GETWITH(30)).widthIs(GETWITH(60)).
    heightEqualToWidth().centerYEqualToView(cell.contentView);
    
    UILabel *gname = [[UILabel alloc] init];
    gname.textColor = UIColorFromRGB(0xb3b3b3);
    gname.font = font14;
    gname.text = dic[@"goods_name"];
    gname.textAlignment = NSTextAlignmentRight;
    [cell.contentView addSubview:gname];
    gname.sd_layout.leftSpaceToView(selectBtn, GETWITH(40)).rightSpaceToView(cell.contentView, GETWITH(60)).centerYEqualToView(cell.contentView).autoHeightRatio(0);
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = _orderInfoArray[indexPath.row];
    CGSize titleSize = [dic[@"goods_name"] boundingRectWithSize:CGSizeMake(GETWITH(300), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font14} context:nil].size;
    return titleSize.height + 30;
}

-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    
    if ([touch.view isDescendantOfView:backView]) {
        return NO;
    }
    
    return YES;
    
}


@end
